package com.itfreer.print.toPdf;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.springframework.stereotype.Component;

import com.itextpdf.text.DocumentException;
import com.itfreer.print.toPdf.WordToPdf;

/**
 * 基于WordToHtml先将word转换为html，再基于HtmlToPdf将html转换为pdf
 */
@Component
public class WordToPdfImp implements WordToPdf {
	
	/**
	 * 将word转换为pdf
	 * 
	 * @param wordFile word文件
	 * @param pdfFile html文件
	 * @param imagePath 图片路径
	 * 
	 * @throws DocumentException 
	 * @throws TransformerException 
	 * @throws ParserConfigurationException 
	 * @throws IOException 
	 */
	public void docToPdf(InputStream wordFile, OutputStream pdfFile, String imagePath)
			throws DocumentException, IOException, ParserConfigurationException, TransformerException{
		
		ByteArrayOutputStream htmlFile = new ByteArrayOutputStream();
		WordToHtmlImp toHtml = new WordToHtmlImp();
		toHtml.docToHtml(wordFile, htmlFile, imagePath);
		
		InputStream in = new ByteArrayInputStream(htmlFile.toByteArray());
		HtmlToPdfImp toPdf = new HtmlToPdfImp();
		toPdf.toPdf(in, pdfFile);
	}
}
